//
//  AY_BookCell1.m
//  AiYue
//
//  Created by boWen on 2023/10/16.
//

#import "AY_BookCell1.h"
#import "AY_BookWeekCell.h"
#import "AY_BookDetailViewController.h"
//本周热门cell
@interface AY_BookCell1 ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UIView * bg_View;//底部圆角

/** 标题  */
@property (strong,nonatomic) UILabel * topTitle;

/** 副标题*/
@property (strong,nonatomic) UILabel * subTitle;

@property (nonatomic, strong) UIButton * benzhouBtn;
@property (strong,nonatomic) UIImageView * benZhouImgView;
@property (strong,nonatomic) UILabel * benZhouTitleLbl;
@property (strong,nonatomic) UILabel * benZhouSubTitleLbl;
@property (strong,nonatomic) UILabel * authorLabel;//作者

@property (nonatomic, strong) UIButton * changeBtn;//换一换
@property (nonatomic, strong) UIImageView * changeImage;//换一换
@property (nonatomic, strong) UICollectionView * myCollectionView;

@property (nonatomic, strong) NSMutableArray * sourceAry;//数据源
@property (strong,nonatomic) UILabel *tipLabel;
@end

@implementation AY_BookCell1

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self addAllSubviewsWithUI];
    }
    return self;
}


- (void)addAllSubviewsWithUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = COLOR_Bg;
    
    self.bg_View = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor whiteColor] andRadius:YNUM(13) andAddSubView:self.contentView];
    [self.bg_View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.contentView);
    }];
    
    
    self.topTitle = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#000002") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(18)] andLines:1 andRadius:0 andAddSubView:self.bg_View];
    [self.topTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View).offset(YNUM(15));
        make.left.equalTo(self.bg_View).offset(YNUM(15));
    }];
    
    self.changeImage = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_change") andBg:COLOR_Bg andRadius:YNUM(4) andAddSubView:self.bg_View];
    self.changeImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(setChangeAction)];
    [self.changeImage addGestureRecognizer:tap];
    [self.changeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View).offset(YNUM(23.5));
        make.right.equalTo(self.bg_View).offset(YNUM(-12));
        make.height.mas_equalTo(YNUM(13));
        make.width.mas_equalTo(YNUM(13));
    }];
    
    self.changeBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"换一换" andFont:YFont(14) andTitleColor:COLOR(@"#999999") andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(setChangeAction) andRadius:0 andAddSubView:self.bg_View];
    [self.changeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View).offset(YNUM(18));
        make.right.equalTo(self.changeImage.mas_left).offset(YNUM(-3));
        make.height.mas_equalTo(YNUM(26));
//        make.width.mas_equalTo(YNUM(100));
    }];
    
    
    self.subTitle = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#909090") andBgColor:nil andFont:YFont(12) andLines:1 andRadius:0 andAddSubView:self.bg_View];
    [self.subTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topTitle.mas_bottom).offset(YNUM(5));
        make.left.equalTo(self.topTitle.mas_left);
    }];
    

    
//    self.benZhouImgView = [AYTool createImageViewWithFrame:CGRectZero andImage:nil andBg:nil andRadius:YNUM(4) andAddSubView:self.bg_View];
//    [self.benZhouImgView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(YNUM(93));
//        make.width.mas_equalTo(YNUM(70));
//        make.left.equalTo(self.bg_View).offset(YNUM(10));
//        make.top.equalTo(self.subTitle.mas_bottom).offset(YNUM(16));
//    }];
    
//    self.tipLabel = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:[UIColor whiteColor] andBgColor:nil andFont:YFont(10) andLines:1 andRadius:YNUM(4) andAddSubView:self.benZhouImgView];
//    self.tipLabel.textAlignment = NSTextAlignmentCenter;
//    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.top.equalTo(self.benZhouImgView);
//        make.width.mas_equalTo(YNUM(32));
//        make.height.mas_equalTo(YNUM(16));
//    }];
    
//    self.benzhouBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:nil andFont:YFont(14) andTitleColor:[UIColor clearColor] andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(clickFirstAction) andRadius:0 andAddSubView:self.bg_View];
//    [self.benzhouBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.benZhouImgView);
//        make.right.equalTo(self.bg_View).offset(YNUM(-12));
//        make.left.equalTo(self.benZhouImgView);
//        make.bottom.equalTo(self.benZhouImgView);
//    }];
//    self.benZhouTitleLbl = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#000002") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(16)] andLines:1 andRadius:0 andAddSubView:self.bg_View];
//    [self.benZhouTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.benZhouImgView.mas_top);
//        make.left.equalTo(self.benZhouImgView.mas_right).offset(YNUM(12));
//        make.right.equalTo(self.bg_View.mas_right).offset(YNUM(-12));
//        make.height.mas_equalTo(YNUM(24));
//    }];
    
//    self.benZhouSubTitleLbl = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#97A4B0") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(12)] andLines:2 andRadius:0 andAddSubView:self.bg_View];
//    [self.benZhouSubTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.benZhouTitleLbl.mas_bottom).offset(YNUM(6));
//        make.left.equalTo(self.benZhouTitleLbl.mas_left);
//        make.right.equalTo(self.bg_View.mas_right).offset(YNUM(-14));
//    }];
    
//    self.authorLabel = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#646D75") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(12)] andLines:1 andRadius:0 andAddSubView:self.bg_View];
//    [self.authorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.benZhouImgView.mas_bottom);
//        make.left.equalTo(self.benZhouTitleLbl.mas_left);
//        make.right.equalTo(self.bg_View.mas_right).offset(YNUM(-14));
//        make.height.mas_equalTo(YNUM(20));
//    }];
    
    [self.bg_View addSubview:self.myCollectionView];
    [self.myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.subTitle.mas_bottom).offset(YNUM(12));
        make.left.equalTo(self.bg_View).offset(YNUM(12));
        make.right.equalTo(self.bg_View.mas_right).offset(YNUM(-12));
        make.bottom.equalTo(self.bg_View).offset(YNUM(-10));
    }];
}

- (UICollectionView *)myCollectionView {
    if (!_myCollectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake((SCREEN_W - YNUM(84))/4, YNUM(135));
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//        layout.minimumLineSpacing = 12;
//        layout.minimumInteritemSpacing = 0;
       UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        collectionView.scrollEnabled = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.backgroundColor = [UIColor whiteColor];
        [collectionView registerClass:[AY_BookWeekCell class] forCellWithReuseIdentifier:@"AY_BookWeekCell"];
        _myCollectionView = collectionView;
    }
    return _myCollectionView;
}

- (NSMutableArray *)sourceAry {
    if(!_sourceAry) {
        _sourceAry = [@[] mutableCopy];
    }
    return _sourceAry;
}

#pragma mark -- UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.sourceAry.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookWeekCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AY_BookWeekCell" forIndexPath:indexPath];
    if (self.sourceAry.count > indexPath.row) {
        cell.subModel = self.sourceAry[indexPath.row];
    }
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookDetailViewController *bookDetailVC = [[AY_BookDetailViewController alloc] init];
    if(self.sourceAry.count > indexPath.row) {
        bookDetailVC.bookId = [self.sourceAry[indexPath.row] bookDecId];
    }
    [[AYTool viewControllerWith:self].navigationController pushViewController:bookDetailVC animated:YES];
}

#pragma mark - 自定义事件

- (void)setChangeAction {
    [AYApiRequest getMallContentRefreshWithControlId:kStringUtils(_model.controlId) success:^(AY_BookRefreshModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            if (!kArrayIsEmpty(pkg.data)) {
                [self.sourceAry removeAllObjects];
                self.sourceAry = [NSMutableArray arrayWithArray:pkg.data];
                [self.myCollectionView reloadData];
            }
        }else {
            
        }
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}

- (void)setModel:(ChannelListModel *)model {
    _model = model;
    self.topTitle.text = kStringUtils(_model.name);
    self.subTitle.text = kStringUtils(_model.bookDescription);
    if(kStringIsEmpty(_model.moreBtnName)){
        self.changeBtn.hidden = YES;
        self.changeImage.hidden = YES;
    }else {
        self.changeBtn.hidden = NO;
        self.changeImage.hidden = NO;
        [self.changeBtn setTitle:kStringUtils(_model.moreBtnName) forState:UIControlStateNormal];
    }
    if(kArrayIsEmpty(model.items)) {
        return;
    }
    self.sourceAry = [NSMutableArray arrayWithArray:_model.items];
    [self.myCollectionView reloadData];
}


- (void)setFrame:(CGRect)frame {
    frame.origin.x += 16;
    frame.size.width -= 2 * 16;
    [super setFrame:frame];
}


@end
